# 分析云清理sql脚本

from comm.db.queryMysql import MysqlServer
import requests

def cleanSql():
    host = 'rm-wz9551asaf60gk0323o.mysql.rds.aliyuncs.com'
    port = 3306
    db= 'mysql'
    user = 'dengh01_rw'
    auth = 'WfKm7gN4jnkfuTRy'

    data = {"ocean":"2023-01-01",
            "tx":"2022-11-23"}
    mysql = MysqlServer(host, int(port), db, user, auth)
    cleanDB = "UPDATE `ad-serving_fangzhiadmin_test`.`ad_account_to_project` SET is_bind = 0 WHERE project_id = '39f6699d-edcd-6c55-0218-2fee2c402547' AND " \
              "account_id not in('24460611','1746082859510861');"
    mysql.execute(cleanDB)
    for platform,dayData in data.items():
        sqlList = [
            "DELETE FROM `ad-serving_fangzhiadmin_test`.`ad_account_stat` WHERE platform='{}'AND day_date ='{}'".format(
                platform, dayData),
            "DELETE FROM `ad-serving_fangzhiadmin_test`.`ad_adcreative_stat` WHERE platform='{}' AND day_date ='{}'".format(
                platform, dayData),
            "DELETE FROM `ad-serving_fangzhiadmin_test`.`ad_adgroup_stat` WHERE platform='{}' AND day_date ='{}'".format(
                platform, dayData),
            "DELETE FROM `ad-serving_fangzhiadmin_test`.`ad_campaign_stat` WHERE platform='{}' AND day_date ='{}'".format(
                platform, dayData),
            # "DELETE FROM `ad-serving_fangzhiadmin_test`.`ad_audiance_info` WHERE platform='{}' AND day_date ='{}'".format(platform,dayData),
            "DELETE FROM `ad-serving_fangzhiadmin_test`.`ad_clue` WHERE platform='{}' AND day_date ='{}'".format(platform,dayData)]

        for sql in sqlList:
            mysql.execute(sql)

def cleanApi():
    """
    :return: 服务器不支持连数据库，通过接口调用本机服务清理数据
    """
    url = "http://10.11.28.56:5007/clean_sql"
    result = requests.get(url)
    print(result)
    return result

